package io.github.wjehovah.yyn.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.utils.ToastUtil;

/**
 * Created by wuyong on 15/8/11.加号的弹出页面
 */
public class AlertDialogActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.send_video)
    private LinearLayout send_video;
    @ViewInject(R.id.send_picture)
    private LinearLayout send_picture;
    @ViewInject(R.id.send_text)
    private LinearLayout send_text;
    @ViewInject(R.id.send_voice)
    private LinearLayout send_voice;
    @ViewInject(R.id.enter_posts)
    private LinearLayout enter_posts;
    @ViewInject(R.id.offline_posts)
    private LinearLayout offline_posts;
    @ViewInject(R.id.iv_icon)
    private ImageView slogan;
    @ViewInject(R.id.cacel)
    private Button cancel;
    private Animation animIn;
    private Animation animOut;
    private ArrayList<View> views;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    for (int i = 0; i < views.size(); i++) {
                        views.get(i).setVisibility(View.VISIBLE);
                        views.get(i).setOnClickListener(AlertDialogActivity.this);
                        animIn = AnimationUtils.loadAnimation(AlertDialogActivity.this, R.anim.in_translate_top);
                        if (i == 0 || i == 2 || i == 5) {
                            animIn.setDuration(animIn.getDuration() - 100);
                        }
                        animIn.setFillAfter(true);
                        views.get(i).startAnimation(animIn);
                    }
                    break;
                case 1:
                    AlertDialogActivity.this.finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.alert_dialog_layout, null);
        ViewUtils.inject(this, view);
        setContentView(view);
        cancel.setOnClickListener(this);
        views = new ArrayList<>();
        views.add(slogan);
        views.add(send_video);
        views.add(send_picture);
        views.add(send_text);
        views.add(send_voice);
        views.add(enter_posts);
        views.add(offline_posts);
        handler.sendEmptyMessageDelayed(0, 100);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cacel:
                cancel.setVisibility(View.GONE);
                for (int i = 0; i < views.size(); i++) {
                    animOut = AnimationUtils.loadAnimation(this, R.anim.out_translate_top);
                    if (i == 0 || i == 2 || i == 5) {
                        animOut.setDuration(animOut.getDuration() + 80);
                    }
                    animOut.setFillAfter(true);
                    views.get(i).startAnimation(animOut);
                    if (i == views.size() - 1) {
                        handler.sendEmptyMessageDelayed(1, 300);
                    }
                }
                break;
            case R.id.send_video:
                TextView video = (TextView) send_video.getChildAt(1);
                ToastUtil.ToastMsg(video.getText());
                break;
            case R.id.send_picture:
                TextView picture = (TextView) send_picture.getChildAt(1);
                ToastUtil.ToastMsg(picture.getText());
                break;
            case R.id.send_text:
                TextView text = (TextView) send_text.getChildAt(1);
                ToastUtil.ToastMsg(text.getText());
                break;
            case R.id.send_voice:
                TextView voice = (TextView) send_voice.getChildAt(1);
                ToastUtil.ToastMsg(voice.getText());
                break;
            case R.id.enter_posts:
                TextView enter = (TextView) enter_posts.getChildAt(1);
                ToastUtil.ToastMsg(enter.getText());
                break;
            case R.id.offline_posts:
                TextView offline = (TextView) offline_posts.getChildAt(1);
                ToastUtil.ToastMsg(offline.getText());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
